home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 October / DPPCPRO1005.ISO / Download / Web Developer / webdeveloper.xpi / chrome / webdeveloper.jar / content / webdeveloper / show_comments.js < prev    next >
Encoding:
Text File  |  2005-03-21  |  7.0 KB  |  225 lines

  1. var webdeveloper_lastCommentShown = null;
  2.  
  3. // Clears the comments from the page
  4. function webdeveloper_clearComments(pageDocument)
  5. {
  6.     var commentIconDiv = null;
  7.     var i              = 0;
  8.  
  9.     webdeveloper_lastCommentShown = null;
  10.  
  11.     // While there is a comment icon div
  12.     while((commentIconDiv = pageDocument.getElementById("webdeveloper-comment-icon" + i)) != null)
  13.     {
  14.         pageDocument.body.removeChild(commentIconDiv);
  15.         pageDocument.body.removeChild(pageDocument.getElementById("webdeveloper-comment-text" + i));
  16.  
  17.         i++;
  18.     }
  19. }
  20.  
  21. // Shows the comment text
  22. function webdeveloper_showCommentText(event)
  23. {
  24.     const target = event.target;
  25.  
  26.     // If there is an event target
  27.     if(target)
  28.     {
  29.         const mainTabBox           = getBrowser().mTabBox;
  30.         const currentDocument      = getBrowser().browsers[mainTabBox.selectedIndex].contentDocument;
  31.         const targetComment        = target.id.replace(new RegExp("icon", "gi"), "text");
  32.         const targetCommentElement = currentDocument.getElementById(targetComment);
  33.  
  34.         var lastCommentElement = null;
  35.  
  36.         // If this is not the last comment shown
  37.         if(webdeveloper_lastCommentShown && webdeveloper_lastCommentShown != targetComment)
  38.         {
  39.             lastCommentElement = currentDocument.getElementById(webdeveloper_lastCommentShown);
  40.  
  41.             // If the last comment element exists
  42.             if(lastCommentElement)
  43.             {
  44.                 lastCommentElement.style.display = "none";
  45.             }
  46.         }
  47.  
  48.         // If the element is currently hidden
  49.         if(targetCommentElement.style.display == "none")
  50.         {
  51.             targetCommentElement.style.display = "block";
  52.         }
  53.         else
  54.         {
  55.             targetCommentElement.style.display = "none";
  56.         }
  57.  
  58.         webdeveloper_lastCommentShown = targetComment;
  59.     }
  60. }
  61.  
  62. // Toggles the comments for a document
  63. function webdeveloper_toggleCommentsForDocument(pageDocument, show)
  64. {
  65.     const treeWalker = pageDocument.createTreeWalker(pageDocument.body, NodeFilter.SHOW_COMMENT, null, false);
  66.  
  67.     var comment        = null;
  68.     var commentsLength = 0;
  69.     var commentsList   = new Array();
  70.     var parentElement  = null;
  71.  
  72.     webdeveloper_lastCommentShown = null;
  73.  
  74.     // While the tree walker has more nodes
  75.     while((comment = treeWalker.nextNode()) != null)
  76.     {
  77.         commentsList.push(comment);
  78.     }
  79.  
  80.     // Remove XML declaration
  81.     if(commentsList.length > 0 && commentsList[0].nodeValue.indexOf("encoding") != -1)
  82.     {
  83.         commentsList.shift();
  84.     }
  85.  
  86.     // Remove DOCTYPE
  87.     if(commentsList.length > 0 && commentsList[0].nodeValue.indexOf("DOCTYPE") != -1)
  88.     {
  89.         commentsList.shift();
  90.     }
  91.  
  92.     commentsLength = commentsList.length;
  93.  
  94.     // If showing comments
  95.     if(show)
  96.     {
  97.         const iconSize = 17;
  98.  
  99.         var commentDiv              = null;
  100.         var commentIconDiv          = null;
  101.         var commentLeft             = 0;
  102.         var commentParent           = null;
  103.         var commentParentOffsetLeft = 0;
  104.         var commentParentOffsetTop  = 0;
  105.         var commentText             = null;
  106.         var commentTop              = 0;
  107.  
  108.         // Loop through the comments
  109.         for(var i = 0; i < commentsLength; i++)
  110.         {
  111.             comment       = commentsList[i];
  112.             commentLeft   = 0;
  113.             commentParent = comment.parentNode;
  114.             commentText   = comment.nodeValue;
  115.             commentTop    = 0;
  116.  
  117.             commentText = commentText.replace(new RegExp("<!--[\s]*", "gi"), "");
  118.             commentText = commentText.replace(new RegExp("[\s]*-->", "gi"), "");
  119.             commentText = commentText.replace(new RegExp("<([^\/][^>]*)class=\"[^\"]*\">", "gi"), "<$1>");
  120.             commentText = commentText.replace(new RegExp("<([^\/][^>]*)class=[^ >]*>", "gi"), "<$1>");
  121.  
  122.             // While the comment has a parent
  123.             while(commentParent)
  124.             {
  125.                 // If the parent node is not a document node or a table row
  126.                 if(commentParent.nodeType != 9 && commentParent.tagName && commentParent.tagName.toLowerCase() != "tr")
  127.                 {
  128.                     commentParentOffsetLeft = commentParent.offsetLeft;
  129.                     commentParentOffsetTop  = commentParent.offsetTop;
  130.  
  131.                     // If the parent node has a left offset
  132.                     if(commentParentOffsetLeft)
  133.                     {
  134.                         commentLeft += commentParentOffsetLeft;
  135.                     }
  136.  
  137.                     // If the parent node has a top offset
  138.                     if(commentParentOffsetTop)
  139.                     {
  140.                         commentTop += commentParentOffsetTop;
  141.                     }
  142.                 }
  143.  
  144.                 commentParent = commentParent.parentNode;
  145.             }
  146.  
  147.             // If the comment left offset is 0
  148.             if(commentLeft == 0)
  149.             {
  150.                 commentLeft = 1;
  151.             }
  152.             else if(commentLeft + iconSize > pageDocument.body.offsetWidth)
  153.             {
  154.                 commentLeft = pageDocument.body.offsetWidth;
  155.             }
  156.  
  157.             // If the comment top offset is 0
  158.             if(commentTop == 0)
  159.             {
  160.                 commentTop = 1;
  161.             }
  162.  
  163.             // If this is not the first comment
  164.             if(i > 0)
  165.             {
  166.                 // Loop through previous comments
  167.                 for(var j = 0; j < i; j++)
  168.                 {
  169.                     // If the top off set of a previous comment matches this comment
  170.                     if(parseInt(pageDocument.getElementById("webdeveloper-comment-icon" + j).style.top) == commentTop)
  171.                     {
  172.                         commentTop += iconSize;
  173.                     }
  174.                 }
  175.             }
  176.  
  177.             commentIconDiv            = pageDocument.createElement("div");
  178.             commentIconDiv.onclick    = webdeveloper_showCommentText;
  179.             commentIconDiv.style.left = commentLeft + "px";
  180.             commentIconDiv.style.top  = commentTop + "px";
  181.  
  182.             commentIconDiv.appendChild(pageDocument.createTextNode("!"));
  183.             commentIconDiv.setAttribute("class", "webdeveloper-comment-icon");
  184.             commentIconDiv.setAttribute("id", "webdeveloper-comment-icon" + i);
  185.  
  186.             pageDocument.body.appendChild(commentIconDiv);
  187.  
  188.             commentDiv            = pageDocument.createElement("div");
  189.             commentDiv.style.left = commentLeft + iconSize + "px";
  190.             commentDiv.style.top  = commentTop + "px";
  191.  
  192.             commentDiv.appendChild(pageDocument.createTextNode(commentText));
  193.             commentDiv.setAttribute("class", "webdeveloper-comment-text");
  194.             commentDiv.setAttribute("id", "webdeveloper-comment-text" + i);
  195.  
  196.             pageDocument.body.appendChild(commentDiv);
  197.  
  198.             // If the offset width is greater than 200
  199.             if(commentDiv.offsetWidth > 200)
  200.             {
  201.                 commentDiv.style.width = "200px";
  202.             }
  203.  
  204.             // If the offset height is greater than 100
  205.             if(commentDiv.offsetHeight > 100)
  206.             {
  207.                 commentDiv.style.height   = "100px";
  208.                 commentDiv.style.overflow = "auto";
  209.             }
  210.  
  211.             // If the comment is positioned outside the document
  212.             if(commentLeft + iconSize + commentDiv.offsetWidth > pageDocument.body.offsetWidth)
  213.             {
  214.                 commentDiv.style.left = commentLeft - commentDiv.offsetWidth - iconSize + "px";
  215.             }
  216.  
  217.             commentDiv.style.display = "none";
  218.         }
  219.     }
  220.     else
  221.     {
  222.         webdeveloper_clearComments(pageDocument);
  223.     }
  224. }
  225.